package com.cykj.pos.controller.api;

import com.cykj.common.core.domain.AjaxResult;
import com.cykj.common.core.domain.entity.SysUser;
import com.cykj.framework.web.service.TokenService;
import com.cykj.pos.domain.BizMerchant;
import com.cykj.pos.enums.bizstatus.CommEnum;
import com.cykj.pos.profit.dto.HomePageDataDTO;
import com.cykj.pos.profit.dto.RankingDTO;
import com.cykj.pos.profit.dto.SysUserDTO;
import com.cykj.pos.profit.dto.TransAmountDataDTO;
import com.cykj.pos.service.IBizMerchTransactionsService;
import com.cykj.pos.service.IBizMerchantService;
import com.cykj.pos.util.DESHelperUtil;
import com.cykj.pos.util.LoginUserUtils;
import com.cykj.system.service.ISysNoticeService;
import com.cykj.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/pos/api/v2/homepage")
@Api(tags = "首页")
public class PosV2AppHomepageController {

    private final IBizMerchantService merchantService;

    private final IBizMerchTransactionsService transRecordsService;

    private final ISysNoticeService noticeService;

    private final TokenService tokenService;

    private final ISysUserService sysUserService;

    @ApiOperation(value = "数据本月统计")
    @PostMapping("/transAmountStatistics")
    public AjaxResult homePageTransAmountData(@RequestBody SysUserDTO userVo) {
        AjaxResult ajaxResult = AjaxResult.success();
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        if (merchant == null) {
            ajaxResult = AjaxResult.success("当前用户未入驻商户");
            ajaxResult.put("code", "500001");
            return ajaxResult;
        }
        Long merchId = merchant.getMerchId();
        TransAmountDataDTO dataVo = merchantService.getTransAmountDataDTO(merchId, userId);
        ajaxResult.put("data", dataVo);
        return ajaxResult;
    }

    @ApiOperation(value = "获取APP首页数据")
    @ApiResponses({@ApiResponse(code = 200, response = HomePageDataDTO.class, message = "业务数据响应成功")})
    @PostMapping("/data")
    public AjaxResult homePageDataList(@RequestBody SysUserDTO userVo) {
        AjaxResult ajaxResult = AjaxResult.success();
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        if (merchant == null) {
            ajaxResult = AjaxResult.success("当前用户未入驻商户");
            ajaxResult.put("code", "500001");
            return ajaxResult;
        }
        Long merchId = merchant.getMerchId();
        /* BigDecimal monthlyTransAmount = transRecordsService.getMonthlyTransAmountByMerchId(merchId); //商户月交易额
        Integer partnerCounts = merchantService.getMonthlyNewPartnerCount(merchId);// 本月新增伙伴
        Integer leafCounts = merchantService.getMonthlyNewMerchCounts(merchId);// 本月新增商户

        String expressNews = noticeService.getExpressNews();
        // 获取用户信息
        SysUser sysUser = sysUserService.selectUserById(userId);
        HomePageDataDTO dataVo = new HomePageDataDTO();
        */
        HomePageDataDTO dataVo = merchantService.getHomePageDataDTO(merchId, userId);
        /*dataVo.setMonthlyNewMerchantCounts(leafCounts);
        dataVo.setMonthlyNewPartnerCounts(partnerCounts);
        dataVo.setMonthlyTransAmount(monthlyTransAmount);
        dataVo.setExpressNews(expressNews);
        dataVo.setNickName(sysUser.getNickName());*/
        dataVo.setMerchCode(merchant.getMerchCode());
        // 腾讯接口对接用的常量
        dataVo.setSecretId(CommEnum.SECRETID.getValue());
        dataVo.setSecretKey(CommEnum.SECRETKEY.getValue());
        dataVo.setBucketName(CommEnum.BUCKETNAME.getValue());
        // 客服电话
        dataVo.setServicePhone(CommEnum.SERVICE_PHONE.getValue());
        // 设置头像
        // dataVo.setPortrait(sysUser.getPortrait());
        ajaxResult.put("data", dataVo);
        return ajaxResult;
    }

    @ApiOperation(value = "我的推荐人")
    @PostMapping("/my/referrer")
    public AjaxResult myReferrer(@RequestBody SysUserDTO userVo) {
        AjaxResult ajaxResult = AjaxResult.success("获得我的推荐人信息成功");
        Long userId = LoginUserUtils.getLoginUserId();
        if (userId == 2) {
            SysUser sysUser = merchantService.getSuperior(2L);
            ajaxResult.put("data", sysUser);
        }
        if (userId != 2) {
            BizMerchant merchant = merchantService.getMerchantByUserId(userId);
            // 通过merch_id查询上级
            BizMerchant merchant1 = merchantService.getMerchantByParentId(merchant.getParentId());
            // 再通过上级的userid去sysUser
            SysUser sysUser = merchantService.getSuperior(merchant1.getUserId());
            ajaxResult.put("data", sysUser);
        }
        return ajaxResult;
    }

    @ApiOperation(value = "获取APP首页数据")
    @ApiResponses({@ApiResponse(code = 200, response = HomePageDataDTO.class, message = "业务数据响应成功")})
    @PostMapping("/dataInfo")
    public AjaxResult homePageDataInfoList(@RequestBody SysUserDTO userVo) {
        AjaxResult ajaxResult = AjaxResult.success();
        Long userId = LoginUserUtils.getLoginUserId();
        BizMerchant merchant = merchantService.getMerchantByUserId(userId);
        if (merchant == null) {
            ajaxResult = AjaxResult.success("当前用户未入驻商户");
            ajaxResult.put("code", "500001");
            return ajaxResult;
        }
        Long merchId = merchant.getMerchId();
        userVo.setMerchId(merchId);
        HomePageDataDTO dataVo = merchantService.getHomePageDataDTOInfo(userVo);
        dataVo.setMerchCode(merchant.getMerchCode());
        dataVo.setIntegral(dataVo.getIntegral());
        dataVo.setWalletAmount(dataVo.getWalletAmount());
        dataVo.setPayPassword(dataVo.getPayPassword());
        ajaxResult.put("data", dataVo);
        return ajaxResult;
    }

    @ApiOperation(value = "获取APP排行榜")
    @PostMapping("/ranking")
    public AjaxResult getRankingList(@RequestBody RankingDTO rankingDTO) {
        AjaxResult ajaxResult = AjaxResult.success("获取排行榜");
        List<RankingDTO> rankingList = merchantService.queryPartnerRankingByTime(rankingDTO);
        ajaxResult.put("data", rankingList);
        return ajaxResult;
    }
}
